{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Title: #String Without AAA or BBB"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Difficulty: #Medium"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Category Title: #Algorithms"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Tag Slug: #greedy #string"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Name Translated: #贪心 #字符串"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solution Name: strWithout3a3b"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Title: #不含 AAA 或 BBB 的字符串"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Content:\n",
    "<p>给定两个整数 <code>a</code>&nbsp;和 <code>b</code>&nbsp;，返回&nbsp;<strong>任意</strong>&nbsp;字符串 <code>s</code>&nbsp;，要求满足：</p>\n",
    "\n",
    "<ul>\n",
    "\t<li><code>s</code>&nbsp;的长度为 <code>a + b</code>，且正好包含&nbsp;<code>a</code>&nbsp;个 <code>'a'</code>&nbsp;字母与&nbsp;<code>b</code> 个 <code>'b'</code>&nbsp;字母；</li>\n",
    "\t<li>子串&nbsp;<code>'aaa'</code>&nbsp;没有出现在 <code>s</code>&nbsp;中；</li>\n",
    "\t<li>子串&nbsp;<code>'bbb'</code> 没有出现在 <code>s</code>&nbsp;中。</li>\n",
    "</ul>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>示例 1：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入：</strong>a = 1, b = 2\n",
    "<strong>输出：</strong>\"abb\"\n",
    "<strong>解释：</strong>\"abb\", \"bab\" 和 \"bba\" 都是正确答案。\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 2：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入：</strong>a = 4, b = 1\n",
    "<strong>输出：</strong>\"aabaa\"</pre>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>提示：</strong></p>\n",
    "\n",
    "<ul>\n",
    "\t<li><code>0 &lt;= a, b&nbsp;&lt;= 100</code></li>\n",
    "\t<li>对于给定的 <code>a</code> 和 <code>b</code>，保证存在满足要求的 <code>s</code>&nbsp;</li>\n",
    "</ul>\n",
    "<span style=\"display:block\"><span style=\"height:0px\"><span style=\"position:absolute\">​​​</span></span></span>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Description: [string-without-aaa-or-bbb](https://leetcode.cn/problems/string-without-aaa-or-bbb/description/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solutions: [string-without-aaa-or-bbb](https://leetcode.cn/problems/string-without-aaa-or-bbb/solutions/)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_cases = ['1\\n2', '4\\n1']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def strWithout3a3b(self, a: int, b: int) -> str:\n",
    "        if a >= b*2:\n",
    "            return 'aab'*b + 'a'*(a-b*2)\n",
    "        elif b >= a*2:\n",
    "            return 'bba'*a + 'b'*(b-a*2)\n",
    "        elif a >= b:\n",
    "            return 'aab'*(a-b) + 'ab'*(2*b-a)\n",
    "        else:\n",
    "            return 'bba'*(b-a) + 'ba'*(2*a-b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def strWithout3a3b(self, a: int, b: int) -> str:\n",
    "        A, B = 'a', 'b'\n",
    "        res = ''\n",
    "        if a < b:\n",
    "            a, b = b, a\n",
    "            A, B = 'b', 'a'\n",
    "        while a and b and a > b:\n",
    "            res += (A + A + B)\n",
    "            a -= 2\n",
    "            b -= 1\n",
    "        while a and b:\n",
    "            res += (A + B)\n",
    "            a -= 1\n",
    "            b -= 1\n",
    "        res += (A * a + B * b)\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def strWithout3a3b(self, a: int, b: int) -> str:\n",
    "        res = ''\n",
    "        while a>0 or b>0:\n",
    "            #print(a, b)\n",
    "            if a>b>0:\n",
    "                res += 'aab'\n",
    "                a -= 2\n",
    "                b -= 1\n",
    "            elif 0<a<b:\n",
    "                res += 'bba'\n",
    "                a -= 1\n",
    "                b -= 2\n",
    "            else:\n",
    "                if a>0:\n",
    "                    res += 'a'\n",
    "                    a -= 1\n",
    "                if b>0:\n",
    "                    res += 'b'\n",
    "                    b -= 1\n",
    "        return res\n",
    "\n",
    "\n",
    "        \n",
    "        mx, mn = max(a, b), min(a, b)\n",
    "        diff = min(mx-mn, mn)\n",
    "        mn -= diff\n",
    "        s1 = 'aab' if a>b else 'bba'\n",
    "        s2 = 'ab' if a>b else 'ba'\n",
    "        if mn==0:\n",
    "            cpy = mx-2*diff\n",
    "            s3 = 'a' if a>b else 'b'\n",
    "            return s1*diff+s3*cpy\n",
    "        return s1*diff+s2*mn\n",
    "\n",
    "\n",
    "\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def strWithout3a3b(self, a: int, b: int) -> str:\n",
    "        res = \"\"\n",
    "        while a > 0 and b > 0:\n",
    "            if a > b:\n",
    "                res += \"aab\"\n",
    "                a -= 2\n",
    "                b -= 1\n",
    "            elif b > a:\n",
    "                res += \"bba\"\n",
    "                a -= 1\n",
    "                b -= 2\n",
    "            else:\n",
    "                res += \"ab\"\n",
    "                a -= 1\n",
    "                b -= 1\n",
    "\n",
    "        while a > 0:\n",
    "            res += \"a\"\n",
    "            a -= 1\n",
    "\n",
    "        while b > 0:\n",
    "            res += \"b\"\n",
    "            b -= 1\n",
    "        return res\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def strWithout3a3b(self, a: int, b: int) -> str:\n",
    "        if a >= b:\n",
    "            answer = ''\n",
    "            while a > b and b > 0:\n",
    "                answer += 'aab'\n",
    "                a -= 2\n",
    "                b -= 1\n",
    "            if a == b:\n",
    "                return answer + 'ab' * a\n",
    "            else:\n",
    "                return answer + 'a' * a\n",
    "        else:\n",
    "            answer = ''\n",
    "            while b > a and a > 0:\n",
    "                answer += 'bba'\n",
    "                b -= 2\n",
    "                a -= 1\n",
    "            if a == b:\n",
    "                return answer + 'ba' * b\n",
    "            else:\n",
    "                return answer + 'b' * b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def strWithout3a3b(self, a: int, b: int) -> str:\n",
    "        pa = a > b\n",
    "        ret = []\n",
    "        while a > 0 or b > 0:\n",
    "            if pa:\n",
    "                c = min(2, a)\n",
    "                if b - a > 0:\n",
    "                    c = min(1, a)    \n",
    "                ret.append(\"a\" * c)\n",
    "                a -= c\n",
    "                pa = False\n",
    "            else:\n",
    "                c = min(b, 2)\n",
    "                if a - b > 0:\n",
    "                    c = min(b, 1)\n",
    "                ret.append(\"b\" * c)\n",
    "                b -= c\n",
    "                pa = True\n",
    "        return \"\".join(ret) "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def strWithout3a3b(self, A: int, B: int) -> str:\n",
    "        ans = []\n",
    "\n",
    "        while A or B:\n",
    "            if len(ans) >= 2 and ans[-1] == ans[-2]:\n",
    "                writeA = ans[-1] == 'b'\n",
    "            else:\n",
    "                writeA = A >= B\n",
    "\n",
    "            if writeA:\n",
    "                A -= 1\n",
    "                ans.append('a')\n",
    "            else:\n",
    "                B -= 1\n",
    "                ans.append('b')\n",
    "\n",
    "        return \"\".join(ans)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def strWithout3a3b(self, a: int, b: int) -> str:\n",
    "        res = ''\n",
    "\n",
    "\n",
    "        while a or b:\n",
    "            if len(res) >= 2 and res[-1]== res[-2]:\n",
    "                writeA = res[-1] =='b'\n",
    "            else:\n",
    "                writeA = a > b\n",
    "            if writeA:\n",
    "                res += 'a'\n",
    "                a -= 1\n",
    "            else:\n",
    "                res += 'b'\n",
    "                b -= 1\n",
    "        return res\n",
    "                "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def strWithout3a3b(self, a: int, b: int) -> str:\n",
    "        pa = a > b\n",
    "        ret = []\n",
    "        while a > 0 or b > 0:\n",
    "            if pa:\n",
    "                c = min(2, a)\n",
    "                if b > a:\n",
    "                    c = min(1, a)    \n",
    "                ret.append(\"a\" * c)\n",
    "                a -= c\n",
    "                pa = False\n",
    "            else:\n",
    "                c = min(b, 2)\n",
    "                if a > b:\n",
    "                    c = min(b, 1)\n",
    "                ret.append(\"b\" * c)\n",
    "                b -= c\n",
    "                pa = True\n",
    "        return \"\".join(ret) "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def strWithout3a3b(self, a: int, b: int) -> str:\n",
    "        l=[1,1]\n",
    "        while a!=0 or b!=0:\n",
    "            if a>=b and (l[-1]!='a' or l[-2]!='a'):\n",
    "                l.append('a')\n",
    "                a-=1\n",
    "            elif a<b and (l[-1]!='b' or l[-2]!='b'):\n",
    "                l.append('b')\n",
    "                b-=1\n",
    "            else:\n",
    "                if l[-1]=='a' and l[-2]=='a':\n",
    "                    l.append('b')\n",
    "                    b-=1\n",
    "                else:\n",
    "                    l.append('a')\n",
    "                    a-=1\n",
    "        l.remove(1)\n",
    "        l.remove(1)\n",
    "        return(''.join(l))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def strWithout3a3b(self, a: int, b: int) -> str:\n",
    "        mx, mn = max(a, b), min(a, b)\n",
    "        diff = min(mx-mn, mn)\n",
    "        mn -= diff\n",
    "        s1 = 'aab' if a>b else 'bba'\n",
    "        s2 = 'ab' if a>b else 'ba'\n",
    "        if mn==0:\n",
    "            cpy = mx-2*diff\n",
    "            s3 = 'a' if a>b else 'b'\n",
    "            return s1*diff+s3*cpy\n",
    "        return s1*diff+s2*mn"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def strWithout3a3b(self, A: int, B: int) -> str:\n",
    "        if A+ B>3:\n",
    "            if A >B:\n",
    "                return 'aab' + self.strWithout3a3b(A-2,B-1)\n",
    "            elif B>A:\n",
    "                return 'bba' + self.strWithout3a3b(A-1,B-2)\n",
    "            else:\n",
    "                return 'ab'*A\n",
    "        else:\n",
    "            return 'a'*A+'b'*B\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\r\n",
    "    def strWithout3a3b(self, a: int, b: int) -> str:\r\n",
    "        if a < b:\r\n",
    "            a, b = b, a\r\n",
    "            x, y = 'b', 'a'\r\n",
    "        else:\r\n",
    "            x, y = 'a', 'b'\r\n",
    "        s = ''\r\n",
    "        while a > 2:\r\n",
    "            n = 1 + (a / b <= 1)\r\n",
    "            s += x * 2 + y * n\r\n",
    "            a -= 2\r\n",
    "            b -= n\r\n",
    "        s += x * a + y * b\r\n",
    "        return s"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def strWithout3a3b(self, a: int, b: int) -> str:\n",
    "        ans = \"\"\n",
    "        if a<3 and b<3:\n",
    "            return 'a'*a+'b'*b\n",
    "        while a>0 and b>0:\n",
    "            if a==b:\n",
    "                ans += \"ab\"*a\n",
    "                a = 0\n",
    "                b = 0\n",
    "            elif a>b and a>=2 and b>=1:\n",
    "                ans+=\"aab\"\n",
    "                a-=2\n",
    "                b-=1\n",
    "            elif a<b and b>=2 and a>=1:\n",
    "                ans += \"bba\"\n",
    "                a-=1\n",
    "                b-=2\n",
    "            else:\n",
    "                break\n",
    "        if a>0:\n",
    "            ans += 'a'*a\n",
    "        if b>0:\n",
    "            ans += 'b'*b\n",
    "        return ans\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def strWithout3a3b(self, a: int, b: int) -> str:\n",
    "        if a == b:\n",
    "            return 'ab' * a\n",
    "\n",
    "        s = ''\n",
    "\n",
    "        while a > 0 or b > 0:\n",
    "            writeA = False\n",
    "            l = len(s)\n",
    "            if l >= 2 and s[-1] == s[-2]:\n",
    "                if s[-1] == 'b':\n",
    "                    writeA = True\n",
    "            else:\n",
    "                if a >= b:\n",
    "                    writeA = True\n",
    "            \n",
    "            if writeA:\n",
    "                a -= 1\n",
    "                s += 'a'\n",
    "            else:\n",
    "                b -= 1\n",
    "                s += 'b'\n",
    "\n",
    "        return s"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def strWithout3a3b(self, a: int, b: int) -> str:\n",
    "        ans = []\n",
    "        A, B = 'a', 'b'\n",
    "        if a < b:\n",
    "            a, b = b, a\n",
    "            A, B = B, A\n",
    "        while a > 0 or b > 0:\n",
    "            if a:\n",
    "                ans.append(A)\n",
    "                a -= 1\n",
    "            if a > b:\n",
    "                ans.append(A)\n",
    "                a -= 1\n",
    "            if b: \n",
    "                ans.append(B)\n",
    "                b -= 1\n",
    "        return ''.join(ans)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def strWithout3a3b(self, a: int, b: int) -> str:\n",
    "        s = []\n",
    "        while a or b:\n",
    "            if len(s) >= 2 and s[-1] == s[-2]:\n",
    "                writeA = s[-1] == 'b'\n",
    "            else:\n",
    "                writeA = a >= b\n",
    "            \n",
    "            if writeA:\n",
    "                a -= 1\n",
    "                s.append('a')\n",
    "            else:\n",
    "                b -= 1\n",
    "                s.append('b')\n",
    "        return ''.join(s)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def strWithout3a3b(self, A: int, B: int) -> str:\n",
    "        if A + B >= 3:\n",
    "            if A > B:\n",
    "                return \"aab\" + self.strWithout3a3b(A-2, B-1)\n",
    "            elif A < B:\n",
    "                return \"bba\" + self.strWithout3a3b(A-1, B-2)\n",
    "            else:\n",
    "                return \"ab\" * A\n",
    "        else:\n",
    "            return 'a' * A + 'b' * B\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def strWithout3a3b(self, a: int, b: int) -> str:\n",
    "        res =\"\"\n",
    "        if a>b:\n",
    "            \n",
    "            while a and b:\n",
    "                if a>b:\n",
    "                    a-=2\n",
    "                    b-=1\n",
    "                    res+=\"aab\"\n",
    "                else:\n",
    "                    a-=1\n",
    "                    b-=1\n",
    "                    res+=\"ab\"\n",
    "            if a:\n",
    "                res+=\"a\"*a\n",
    "        else:\n",
    "            while a and b:\n",
    "                if a<b:\n",
    "                    a-=1\n",
    "                    b-=2\n",
    "                    res+=\"bba\"\n",
    "                else:\n",
    "                    a-=1\n",
    "                    b-=1\n",
    "                    res+=\"ba\"\n",
    "            if b :\n",
    "                res+=\"b\"*b\n",
    "        return res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def strWithout3a3b(self, a: int, b: int) -> str:\n",
    "        ans = []\n",
    "        while a or b:\n",
    "            if len(ans) >= 2 and ans[-1] == ans[-2]:\n",
    "                writeA = ans[-1] == \"b\"\n",
    "            else:\n",
    "                writeA = a >= b\n",
    "            if writeA:\n",
    "                a -= 1\n",
    "                ans.append(\"a\")\n",
    "            else:\n",
    "                b -= 1\n",
    "                ans.append(\"b\")\n",
    "        return \"\".join(ans)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def strWithout3a3b(self, a: int, b: int) -> str:\n",
    "        ans = ''\n",
    "        if a >= b * 2:\n",
    "            return 'aab' * b + 'a' * (a - 2 * b)\n",
    "        elif b >= a * 2:\n",
    "            return 'bba' * a + 'b' * (b - 2 * a)\n",
    "        elif a >= b:\n",
    "            return 'aab' * (a - b) + 'ab' * (2 * b - a)\n",
    "        else:\n",
    "            return 'bba' * (b - a) + 'ba' * (2 * a - b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def strWithout3a3b(self, a: int, b: int) -> str:\n",
    "        ans = ''\n",
    "        while a or b:\n",
    "            if len(ans) >= 2 and ans[-1] == ans[-2]:\n",
    "                A = ans[-1] == 'b'\n",
    "            else:\n",
    "                A = a >= b\n",
    "            \n",
    "            if A:\n",
    "                ans += 'a'\n",
    "                a -= 1\n",
    "            else:\n",
    "                ans += 'b'\n",
    "                b -= 1\n",
    "        \n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def strWithout3a3b(self, a: int, b: int) -> str:\n",
    "        s = \"\"\n",
    "        while a>0 or b>0:\n",
    "            if a > b:\n",
    "                if a >= 2 and b >= 1:\n",
    "                    s = s + \"aab\"\n",
    "                    a -= 2\n",
    "                    b -= 1\n",
    "                elif b == 0:\n",
    "                    s = s + \"a\"\n",
    "                    a -= 1\n",
    "            if b > a:\n",
    "                if b >= 2 and a >= 1:\n",
    "                    s = s + \"bba\"\n",
    "                    b -= 2\n",
    "                    a -= 1\n",
    "                elif a == 0:\n",
    "                    s = s + \"b\"\n",
    "                    b -= 1\n",
    "            if a == b and a > 0:\n",
    "                    s = s + \"a\"\n",
    "                    a -= 1\n",
    "        return s\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def strWithout3a3b(self, a: int, b: int) -> str:\n",
    "\n",
    "        if a>=b:\n",
    "            x=\"a\"\n",
    "            y=\"b\"\n",
    "            nx=a\n",
    "            ny=b\n",
    "        else:\n",
    "            x=\"b\"\n",
    "            y=\"a\"\n",
    "            nx=b\n",
    "            ny=a\n",
    "        countx=nx//2+nx%2\n",
    "        county=countx\n",
    "        s=\"\"\n",
    "        while countx >0 and county>0:\n",
    "            s=s+x*2\n",
    "            countx-=1\n",
    "            if ny>county:\n",
    "                s=s+y*2\n",
    "                county-=1\n",
    "                ny=ny-2\n",
    "            elif ny==county:\n",
    "                s=s+y*1\n",
    "                county-=1\n",
    "                ny=ny-1\n",
    "            elif ny<=county:\n",
    "                s=s+y*1\n",
    "                county-=1\n",
    "                ny=ny-1\n",
    "        if s.count(y)>min(a,b):\n",
    "            s=s[:-1]\n",
    "        if s.count(x)>max(a,b):\n",
    "            s=s[1:]\n",
    "        return s\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def strWithout3a3b(self, a: int, b: int) -> str:\n",
    "        \"\"\"\n",
    "        三个字母为一组 一组里面要么2a 1b 要么2b 1a\n",
    "        那么取2a还是2b呢 根据当前数量来定 哪个多就哪个取2个\n",
    "        如果数量相等就插入ab\n",
    "        永远不会出现aaa和bbb 因为如果一开始a>b 几次循环后有a=b就会插入ab 或者a>0&b=0 且此时a个数一定小于等于2 \n",
    "        这一点是题目的正确性给出的\n",
    "        \"\"\"\n",
    "        rs = []\n",
    "       \n",
    "        while a>0 and b>0:\n",
    "            if a>b:\n",
    "                rs.append(\"aab\")\n",
    "                a -= 2\n",
    "                b -= 1\n",
    "            elif a==b:\n",
    "                rs.append(\"ab\")\n",
    "                a -= 1\n",
    "                b -= 1  \n",
    "            else:\n",
    "                rs.append(\"bba\")\n",
    "                a -= 1\n",
    "                b -= 2 \n",
    "        if a:\n",
    "            rs.append(\"a\"*a)\n",
    "        if b:\n",
    "            rs.append(\"b\"*b)\n",
    "        return \"\".join(rs)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def strWithout3a3b(self, a: int, b: int) -> str:\n",
    "        rst = ''\n",
    "        if a>b:\n",
    "            while a>b and a>1 and b>0:\n",
    "                rst += 'aab'\n",
    "                a -= 2\n",
    "                b -= 1\n",
    "\n",
    "            while a>0 and b>0:\n",
    "                rst += 'ab'\n",
    "                a -= 1\n",
    "                b -= 1\n",
    "            rst += 'a'*a + 'b'* b\n",
    "        else:\n",
    "            while b>a and b>1 and a>0:\n",
    "                rst += 'bba'\n",
    "                a -= 1\n",
    "                b -= 2\n",
    "            while a>0 and b>0:\n",
    "                rst += 'ba'\n",
    "                a -= 1\n",
    "                b -= 1\n",
    "            rst += 'b'* b + 'a'*a \n",
    "\n",
    "        return rst\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def strWithout3a3b(self, a: int, b: int) -> str:\n",
    "        mx, mn = max(a, b), min(a, b)\n",
    "        diff = min(mx-mn, mn)\n",
    "        mn -= diff\n",
    "        s1 = 'aab' if a>b else 'bba'\n",
    "        s2 = 'ab' if a>b else 'ba'\n",
    "        if mn==0:\n",
    "            cpy = mx-2*diff\n",
    "            s3 = 'a' if a>b else 'b'\n",
    "            return s1*diff+s3*cpy\n",
    "        return s1*diff+s2*mn"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def strWithout3a3b(self, a: int, b: int) -> str:\n",
    "        s = \"\"\n",
    "        while a>0 or b>0:\n",
    "            if a > b:\n",
    "                if a >= 2 and b >= 1:\n",
    "                    s = s + \"aab\"\n",
    "                    a -= 2\n",
    "                    b -= 1\n",
    "                elif b == 0:\n",
    "                    s = s + \"a\"\n",
    "                    a -= 1\n",
    "            if b > a:\n",
    "                if b >= 2 and a >= 1:\n",
    "                    s = s + \"bba\"\n",
    "                    b -= 2\n",
    "                    a -= 1\n",
    "                elif a == 0:\n",
    "                    s = s + \"b\"\n",
    "                    b -= 1\n",
    "            if a == b and a > 0:\n",
    "                    s = s + \"a\"\n",
    "                    a -= 1\n",
    "        return s\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution(object):\n",
    "    def strWithout3a3b(self, A, B):\n",
    "        ans = []\n",
    "\n",
    "        while A or B:\n",
    "            if len(ans) >= 2 and ans[-1] == ans[-2]:\n",
    "                writeA = ans[-1] == 'b'\n",
    "            else:\n",
    "                writeA = A >= B\n",
    "\n",
    "            if writeA:\n",
    "                A -= 1\n",
    "                ans.append('a')\n",
    "            else:\n",
    "                B -= 1\n",
    "                ans.append('b')\n",
    "\n",
    "        return \"\".join(ans)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def strWithout3a3b(self, a: int, b: int) -> str:\n",
    "        res = \"\"\n",
    "\n",
    "        if a >= b:\n",
    "            more, n_more = 'a', a\n",
    "            less, n_less = 'b', b\n",
    "        else:\n",
    "            more, n_more = 'b', b\n",
    "            less, n_less = 'a', a\n",
    "\n",
    "        if n_more > 2 * n_less:\n",
    "            res += more * (n_more - 2 * n_less)\n",
    "            n_more = 2 * n_less\n",
    "\n",
    "        diff = n_more - n_less\n",
    "        if diff >= 1:\n",
    "            res += (less + more*2) * diff\n",
    "\n",
    "        res += (less + more) * (n_less - diff)\n",
    "\n",
    "        return res\n",
    "             \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def strWithout3a3b(self, a: int, b: int) -> str:\n",
    "        if a == b: return 'ab' * a  \n",
    "        la = []\n",
    "        while a > 0 and b > 0:\n",
    "\n",
    "            if a > b:\n",
    "                la.append('aab')\n",
    "                a -= 2\n",
    "                b -= 1\n",
    "\n",
    "            elif a < b:\n",
    "                la.append('bba')\n",
    "                a -= 1\n",
    "                b -= 2\n",
    "\n",
    "            elif a == b:\n",
    "                la.append('ab' * a) \n",
    "                a = b = 0\n",
    "\n",
    "        return ''.join(la) + a * 'a' + b * 'b'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def strWithout3a3b(self, a: int, b: int) -> str:\n",
    "        if a == 1 and b == 1:   return \"ab\"\n",
    "\n",
    "        ans = []\n",
    "        if a >= b:\n",
    "            while not (2 * b <= a <= 2 * b + 2):\n",
    "                ans.append(\"abb\")\n",
    "                a -= 1\n",
    "                b -= 2\n",
    "            return \"\".join(ans) + \"aab\" * b + \"a\" * (a - 2 * b)\n",
    "        else:\n",
    "            while not (2 * a <= b <= 2 * a + 2):\n",
    "                ans.append(\"baa\")\n",
    "                a -= 2\n",
    "                b -= 1\n",
    "            return \"\".join(ans) + \"bba\" * a + \"b\" * (b - 2 * a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def strWithout3a3b(self, A: int, B: int) -> str:\n",
    "        if A + B >= 3:\n",
    "            if A > B:\n",
    "                return \"aab\" + self.strWithout3a3b(A-2, B-1)\n",
    "            elif A < B:\n",
    "                return \"bba\" + self.strWithout3a3b(A-1, B-2)\n",
    "            else:\n",
    "                return \"ab\" * A\n",
    "        else:\n",
    "            return 'a' * A + 'b' * B"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def strWithout3a3b(self, a: int, b: int) -> str:\n",
    "        ans = ['', '']\n",
    "        while a > 0 or b > 0:\n",
    "            if a > b:\n",
    "                if ans[-2:] != ['a', 'a']:\n",
    "                    ans.append('a')\n",
    "                    a -= 1\n",
    "                else:\n",
    "                    ans.append('b')\n",
    "                    b -= 1\n",
    "            else:\n",
    "                if ans[-2:] != ['b', 'b']:\n",
    "                    ans.append('b')\n",
    "                    b -= 1\n",
    "                else:\n",
    "                    ans.append('a')\n",
    "                    a -= 1\n",
    "        return ''.join(ans)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def strWithout3a3b(self, a: int, b: int) -> str:\n",
    "        res = ''\n",
    "        while a > 0 or b > 0:\n",
    "            if a > b:\n",
    "                if len(res) > 0 and res[-2:] == 'aa':\n",
    "                    b -= 1\n",
    "                    res += 'b'\n",
    "                else:\n",
    "                    a -= 1\n",
    "                    res += 'a'\n",
    "            else:\n",
    "                if len(res) > 0 and res[-2:] == 'bb':\n",
    "                    a -= 1\n",
    "                    res += 'a'\n",
    "                else:\n",
    "                    b -= 1\n",
    "                    res += 'b'\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def strWithout3a3b(self, a: int, b: int) -> str:\n",
    "        l=[]\n",
    "        while a or b:\n",
    "            if len(l)>=2 and l[-1]==l[-2]:\n",
    "                writeA= l[-1]=='b'\n",
    "            else:\n",
    "                writeA=a>=b\n",
    "            if writeA:\n",
    "                l.append('a')\n",
    "                a-=1\n",
    "            else:\n",
    "                l.append('b')\n",
    "                b-=1\n",
    "        return(''.join(l))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def strWithout3a3b(self, a: int, b: int) -> str:\n",
    "        num = min(a, b)\n",
    "        diff = min(abs(a-b), num)\n",
    "        num -= diff\n",
    "        s1 = 'aab' if a>b else 'bba'\n",
    "        s2 = 'ab' if a<b else 'ba'\n",
    "        s3 = ''\n",
    "        if num==0:\n",
    "            cpy = max(a, b)-2*diff\n",
    "            s3 = 'a'*cpy if a>b else 'b'*cpy\n",
    "        return s1*diff+s2*num+s3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def strWithout3a3b(self, A: int, B: int) -> str:\n",
    "        if A + B >= 3:\n",
    "            if A > B:\n",
    "                return \"aab\" + self.strWithout3a3b(A-2, B-1)\n",
    "            elif A < B:\n",
    "                return \"bba\" + self.strWithout3a3b(A-1, B-2)\n",
    "            else:\n",
    "                return \"ab\" * A\n",
    "        else:\n",
    "            return 'a' * A + 'b' * B\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def strWithout3a3b(self, a: int, b: int) -> str:\n",
    "        res = ''\n",
    "        while a>0 or b>0:\n",
    "            #print(a, b)\n",
    "            if a>b>0:\n",
    "                res += 'aab'\n",
    "                a -= 2\n",
    "                b -= 1\n",
    "            elif 0<a<b:\n",
    "                res += 'bba'\n",
    "                a -= 1\n",
    "                b -= 2\n",
    "            else:\n",
    "                if a>0:\n",
    "                    res += 'a'\n",
    "                    a -= 1\n",
    "                if b>0:\n",
    "                    res += 'b'\n",
    "                    b -= 1\n",
    "        return res\n",
    "\n",
    "\n",
    "        \n",
    "        mx, mn = max(a, b), min(a, b)\n",
    "        diff = min(mx-mn, mn)\n",
    "        mn -= diff\n",
    "        s1 = 'aab' if a>b else 'bba'\n",
    "        s2 = 'ab' if a>b else 'ba'\n",
    "        if mn==0:\n",
    "            cpy = mx-2*diff\n",
    "            s3 = 'a' if a>b else 'b'\n",
    "            return s1*diff+s3*cpy\n",
    "        return s1*diff+s2*mn\n",
    "\n",
    "\n",
    "\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def strWithout3a3b(self, a: int, b: int) -> str:\n",
    "        n1, n2 = max(a, b), min(a, b)\n",
    "        if a >= b: c1, c2 = 'a', 'b'\n",
    "        else: c1, c2 = 'b', 'a'\n",
    "        ans = ''\n",
    "        while n1 > n2:\n",
    "            if n1 > 0:\n",
    "                ans += c1\n",
    "                n1 -= 1\n",
    "                if n1 > 0:\n",
    "                    ans += c1\n",
    "                    n1 -= 1\n",
    "            if n2 > 0:\n",
    "                ans += c2\n",
    "                n2 -= 1\n",
    "        while n1 > 0:\n",
    "            ans += c1\n",
    "            ans += c2\n",
    "            n1 -= 1\n",
    "        return ans\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def strWithout3a3b(self, a: int, b: int) -> str:\n",
    "        \n",
    "        # a > b\n",
    "        def dp(a, b, ta, tb):\n",
    "            res = [ta + tb] * b\n",
    "            a -= b\n",
    "\n",
    "            for i in range(len(res)):\n",
    "                if a > 0:\n",
    "                    res[i] = ta + res[i]\n",
    "                    a -= 1\n",
    "                    if a == 0:\n",
    "                        break\n",
    "            \n",
    "            return ''.join(res) + ta * a \n",
    "\n",
    "        \n",
    "        if a > b:\n",
    "            return dp(a, b, 'a', 'b')\n",
    "        else:\n",
    "            return dp(b, a, 'b', 'a')\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def strWithout3a3b(self, a: int, b: int) -> str:\n",
    "        if a == b:\n",
    "            return \"ab\"*a\n",
    "        ans = \"\"\n",
    "        la,lb = a,b\n",
    "        while la > 0 and lb > 0:\n",
    "            if la > lb:\n",
    "                ans += \"aab\"\n",
    "                la -= 2\n",
    "                lb -= 1\n",
    "            elif la < lb:\n",
    "                ans += \"bba\"\n",
    "                la -= 1\n",
    "                lb -= 2\n",
    "            else:\n",
    "                if a > b:\n",
    "                    ans += \"ab\" * la\n",
    "                else:\n",
    "                    ans += \"ba\" * lb\n",
    "                la = 0\n",
    "                lb = 0\n",
    "        if a > b:\n",
    "            ans += \"a\"* la + \"b\" * lb\n",
    "        else:\n",
    "            ans += \"b\" * lb + \"a\" * la\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def strWithout3a3b(self, a: int, b: int) -> str:\n",
    "        ret=\"\"\n",
    "        if b>a and b>=2 and b-a>=2:\n",
    "            b-=2\n",
    "            ret+='bb'\n",
    "        while a>0 and b>0:\n",
    "            if a==b:\n",
    "                ret+=\"ab\"*a\n",
    "                a=0\n",
    "                b=0\n",
    "            elif a>b:\n",
    "                a-=2\n",
    "                b-=1\n",
    "                ret+=\"aab\"\n",
    "            else:\n",
    "                b-=2\n",
    "                a-=1\n",
    "                ret+=\"abb\"\n",
    "        if a:\n",
    "            ret+=\"a\"*a\n",
    "        return ret"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def strWithout3a3b(self, a: int, b: int) -> str:\n",
    "        str1 = \"\"\n",
    "        while (a != 0 and b != 0):\n",
    "            if a > b:\n",
    "                str1 += \"aab\"\n",
    "                a -= 2\n",
    "                b -= 1\n",
    "            elif a < b:\n",
    "                str1 += \"bba\"\n",
    "                a -= 1\n",
    "                b -= 2\n",
    "            else:\n",
    "                str1 += \"ab\"\n",
    "                a -= 1\n",
    "                b -= 1\n",
    "        if a != 0:\n",
    "            str1 += 'a' * a\n",
    "        elif b != 0:\n",
    "            str1 += 'b' * b\n",
    "        return str1\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def strWithout3a3b(self, a: int, b: int) -> str:\n",
    "        pua=0\n",
    "        pub=0\n",
    "        m=a+b\n",
    "        ans=\"\"\n",
    "        while len(ans)<m:\n",
    "            if pua>=2:\n",
    "                ans+=\"b\"\n",
    "                b-=1\n",
    "                pua=0\n",
    "                pub+=1\n",
    "            elif pub>=2:\n",
    "                ans+=\"a\"\n",
    "                a-=1\n",
    "                pub=0\n",
    "                pua+=1 \n",
    "            elif a>=b and pua<2:\n",
    "                ans+=\"a\"\n",
    "                a-=1\n",
    "                pua+=1\n",
    "                pub=0\n",
    "            elif a<=b and pub<2:\n",
    "                ans+=\"b\"\n",
    "                b-=1\n",
    "                pub+=1\n",
    "                pua=0\n",
    "        return ans        \n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def strWithout3a3b(self, a: int, b: int) -> str:\n",
    "        if a == 0:\n",
    "            return 'b'*b\n",
    "        if b == 0:\n",
    "            return 'a'*a\n",
    "        if a == b:\n",
    "            return 'ab' + self.strWithout3a3b(a-1,b-1)\n",
    "        elif a > b:\n",
    "            return 'aab' + self.strWithout3a3b(a-2,b-1)\n",
    "        else:\n",
    "            return 'bba' + self.strWithout3a3b(a-1,b-2)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def strWithout3a3b(self, a: int, b: int) -> str:\n",
    "        res = []\n",
    "        while True:\n",
    "            if a == b == 0: break\n",
    "            if a == 0:\n",
    "                res.append('b' * b)\n",
    "                break\n",
    "            elif b == 0:\n",
    "                res.append('a' * a)\n",
    "                break\n",
    "            elif a > b:\n",
    "                res.append('aab')\n",
    "                a -= 2\n",
    "                b -= 1\n",
    "            elif a < b:\n",
    "                res.append('bba')\n",
    "                a -= 1\n",
    "                b -= 2\n",
    "            else:\n",
    "                res.append('ab' * a)\n",
    "                break\n",
    "        return ''.join(res)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def strWithout3a3b(self, a: int, b: int) -> str:\n",
    "        #交叉给 a > b时  aab a = b后 ab\n",
    "        ans = ''\n",
    "        if a> b:\n",
    "            while a>b:\n",
    "                ans += 'a'\n",
    "                a -= 1\n",
    "                if a == b:return ans+ 'ab'*a\n",
    "                if b == 0:return ans+ 'a'*a\n",
    "                ans += 'a'\n",
    "                a -= 1\n",
    "                ans += 'b'\n",
    "                b -= 1\n",
    "        elif a< b:\n",
    "            while a<b:\n",
    "                ans += 'b'\n",
    "                b -= 1\n",
    "                if a == b:return ans+ 'ba'*b\n",
    "                if a == 0:return ans+ 'b'*b\n",
    "                ans += 'b'\n",
    "                b -= 1\n",
    "                ans += 'a'\n",
    "                a -= 1\n",
    "        return ans+'ab'*a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def strWithout3a3b(self, a: int, b: int) -> str:\n",
    "        if a == b:\n",
    "            return \"ab\" * a\n",
    "        sa, sb = ('a', 'b') if a > b else ('b', 'a')\n",
    "        if b > a:\n",
    "            a, b = b, a\n",
    "        if a - b < 2:\n",
    "            return \"ab\" * b + sa\n",
    "        s1 = sa + sa + sb\n",
    "        s2 = sa + sb\n",
    "        s3 = sa * 2\n",
    "\n",
    "        n1 = a - b - 2\n",
    "        n2 = 2 * b - a + 2\n",
    "        return s1 * n1 + s2 * n2 + s3\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def strWithout3a3b(self, a: int, b: int) -> str:\n",
    "        ans = \"\"\n",
    "        if a<3 and b<3:\n",
    "            return 'a'*a+'b'*b\n",
    "        while a>0 and b>0:\n",
    "            flag = (max(a,b)==a)\n",
    "            if a==b:\n",
    "                ans += \"ab\"*a\n",
    "                a = 0\n",
    "                b = 0\n",
    "            elif flag and a>=2 and b>=1:\n",
    "                ans+=\"aab\"\n",
    "                a-=2\n",
    "                b-=1\n",
    "            elif not flag and b>=2 and a>=1:\n",
    "                ans += \"bba\"\n",
    "                a-=1\n",
    "                b-=2\n",
    "            else:\n",
    "                break\n",
    "        if a>0:\n",
    "            ans += 'a'*a\n",
    "        if b>0:\n",
    "            ans += 'b'*b\n",
    "        return ans\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def strWithout3a3b(self, A: int, B: int) -> str:\n",
    "        ans = []\n",
    "\n",
    "        while A or B:\n",
    "            if len(ans) >= 2 and ans[-1] == ans[-2]:\n",
    "                writeA = ans[-1] == 'b'\n",
    "            else:\n",
    "                writeA = A >= B\n",
    "\n",
    "            if writeA:\n",
    "                A -= 1\n",
    "                ans.append('a')\n",
    "            else:\n",
    "                B -= 1\n",
    "                ans.append('b')\n",
    "\n",
    "        return \"\".join(ans)\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def strWithout3a3b(self, a: int, b: int) -> str:\n",
    "        res = []\n",
    "        while a > b and b > 0: \n",
    "            a -= 2\n",
    "            b -= 1\n",
    "            res.append(\"aab\")\n",
    "        while b > a and a > 0:\n",
    "            b -= 2\n",
    "            a -= 1\n",
    "            res.append(\"bba\")\n",
    "        while a > 0 and b > 0:\n",
    "            a -= 1\n",
    "            b -= 1\n",
    "            res.append(\"ab\")\n",
    "        while a > 0:\n",
    "            a -= 1\n",
    "            res.append(\"a\")\n",
    "        while b > 0:\n",
    "            b -= 1\n",
    "            res.append(\"b\")\n",
    "        return \"\".join(res)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def strWithout3a3b(self, a: int, b: int) -> str:\n",
    "        ans = ''\n",
    "        while a >= 1 and b >= 1:\n",
    "            if a > b:\n",
    "                ans += 'aab'\n",
    "                a -= 1\n",
    "            elif a < b:\n",
    "                ans += 'bba'\n",
    "                b -= 1\n",
    "            else:\n",
    "                ans += 'ab'\n",
    "            a -= 1\n",
    "            b -= 1\n",
    "\n",
    "        while b > 0:\n",
    "            ans += 'b'\n",
    "            b -= 1\n",
    "        while a > 0:\n",
    "            ans += 'a'\n",
    "            a -= 1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def strWithout3a3b(self, a: int, b: int) -> str:\n",
    "        c1,c2 = 'a','b'\n",
    "        num1,num2 = 0, 0\n",
    "        if a>b:\n",
    "            c1,c2 = 'a','b'\n",
    "            num1,num2 = a, b\n",
    "        else:\n",
    "            c1,c2 = 'b','a'\n",
    "            num1,num2 = b,a\n",
    "\n",
    "        res = \"\"\n",
    "        while len(res)<a+b:\n",
    "            if (num2-1)<=(num1-1)<=2*(num2-1)+2:\n",
    "                if num1>0:\n",
    "                    num1 -= 1\n",
    "                    res = res + c1\n",
    "                if num2>0:\n",
    "                    num2 -= 1\n",
    "                    res = res + c2\n",
    "            else:\n",
    "                if num1>=2:\n",
    "                    num1 -= 2\n",
    "                    res = res + c1 + c1\n",
    "                else:\n",
    "                    num1 -= 1\n",
    "                    res = res + c1\n",
    "                if num2>0:\n",
    "                    num2 -= 1\n",
    "                    res = res + c2\n",
    "            pass\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def strWithout3a3b(self, a: int, b: int) -> str:\n",
    "        count = 0\n",
    "        low_a = a // 2 + a % 2\n",
    "        low_b = b // 2 + b % 2\n",
    "        for i in range(low_a, a+1):\n",
    "            if i >= low_b-1 and i <= b+1:\n",
    "                count = i\n",
    "                break\n",
    "        \n",
    "        if count in range(low_a, a+1):\n",
    "            count_a = count\n",
    "        elif count > a:\n",
    "            count_a = a\n",
    "        else:\n",
    "            count_a = low_a\n",
    "\n",
    "        if count in range(low_b, b+1):\n",
    "            count_b = count\n",
    "        elif count > b:\n",
    "            count_b = b\n",
    "        else:\n",
    "            count_b = low_b\n",
    "\n",
    "        #print(low_a,a,low_b,b)\n",
    "        #print(count, count_a, count_b)\n",
    "        list_a = []\n",
    "        for i in range(a - count_a):\n",
    "            list_a.append(\"aa\")\n",
    "        for i in range(2*count_a - a):\n",
    "            list_a.append('a')\n",
    "\n",
    "        list_b = []\n",
    "        for i in range(b - count_b):\n",
    "            list_b.append(\"bb\")\n",
    "        for i in range(2*count_b - b):\n",
    "            list_b.append('b')\n",
    "        print(list_a, list_b)\n",
    "        final = \"\"\n",
    "        n_a = len(list_a)\n",
    "        n_b = len(list_b)\n",
    "        if n_a == n_b:\n",
    "            for i in range(n_a):\n",
    "                final += list_a[i]\n",
    "                final += list_b[i]\n",
    "        elif n_a < n_b:\n",
    "            for i in range(n_a):\n",
    "                final += list_b[i]\n",
    "                final += list_a[i]\n",
    "            final += list_b[-1]\n",
    "        elif n_a > n_b:\n",
    "            for i in range(n_b):\n",
    "                final += list_a[i]\n",
    "                final += list_b[i]\n",
    "            final += list_a[-1]\n",
    "        return final"
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 2
}
